package org.thanos.demo;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MosquittoApp {

    @Value("${spring.mqtt.broker}")
    private String broker;

    @Value("${spring.mqtt.clientId}")
    private String clientId;

    public static void main(String[] args) {
        new SpringApplicationBuilder(MosquittoApp.class)
                .web(WebApplicationType.NONE)
                .run(args);
    }


    @Bean(name = "mqttClient")
    public MqttClient mqttClient() throws MqttException {
        MqttClient mqttClient = new MqttClient(broker, clientId, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setConnectionTimeout(10);
        options.setKeepAliveInterval(20);
        options.setMaxInflight(10000);
        mqttClient.connect(options);
        return mqttClient;
    }

}
